package com.qf.dao.impl;

import com.qf.dao.AccountDao;
import com.qf.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AccountDaoImpl
 * @Description TODO
 * @Author 86181
 * @Date 2022/8/30 23:26
 * @Version 1.0
 **/
@Repository(value = "accountDaoImpl")
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private Connection conn;
    public List<Account> findAllAccount() {
        PreparedStatement statement=null;
        try {
            statement = conn.prepareStatement("select * from account");
            ResultSet resultSet = statement.executeQuery();
            ArrayList<Account> list = new ArrayList<Account>();
            while (resultSet.next()) {
                Account account = new Account();
                account.setId(resultSet.getInt("ID"));
                account.setName(resultSet.getString("MAME"));
                account.setMoney(resultSet.getDouble("MOMEY"));
                list.add(account);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            try {
                statement.close();
                conn.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

        }

    }
}
